﻿using HuaweiCloud.Dotnet.Samples.ESports.Main.Dtos;
using HuaweiCloud.Dotnet.Samples.ESports.Main.Manager;
using HuaweiCloud.Dotnet.Samples.ESports.Main.Service;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Guids;

namespace HuaweiCloud.Dotnet.Samples.ESports.Main.Controllers
{
    [Route("api-main/v1/[controller]")]
    [Route("api/v1/[controller]")]
    [ApiController]
    public class DeviceController : ControllerBase
    {
        private readonly IDeviceService _service;
        private readonly IGuidGenerator _idGenerator;
        private readonly IDeviceManager _manager;
        public DeviceController(IDeviceService service
            , IGuidGenerator idGenerator
            , IDeviceManager manager)
        {
            _service = service;
            _idGenerator = idGenerator;
            _manager = manager;
        }

        [HttpGet]
        public async Task<ActionResult<IEnumerable<DeviceDto>>> Get()
        {
            return Ok(await _service.ListAsync());
        }

        [HttpGet("my/{userId}")]
        public async Task<ActionResult<IEnumerable<DeviceDto>>> MyGet(Guid userId)
        {
            return await _manager.GetDevicesByUserIdAsync(userId);
        }


        [HttpGet("{id}")]
        public async Task<ActionResult<DeviceDto>> Get(Guid id)
        {
            var item = await _service.GetAsync(id);
            return item == null ? NotFound() : Ok(item);
        }

        [HttpPost]
        public async Task<ActionResult> Post([FromBody] CreateDeviceDto dto)
        {
            dto.Id = _idGenerator.Create();
            await _service.AddAsync(dto);
            return Created("",null);
        }

        [HttpPut("{id}")]
        public async Task<ActionResult> Put(Guid id, [FromBody] UpdateDeviceDto dto)
        {
            dto.Id = id;
            await _service.UpdateAsync(dto);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult> Delete(Guid id)
        {
            await _service.DeleteAsync(id);
            return NoContent();
        }
    }
}
